package com.sojson.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sojson.config.exception.TransException;
import com.sojson.dao.DictDataDao;
import com.sojson.dao.DictTypeDao;
import com.sojson.entity.po.DictType;
import com.sojson.service.DictTypeService;

/**
 * 字典类型表Service实现类
 * 
 * @author liu
 * @date 2021-04-13
 */
@Service
public class DictTypeServiceImpl implements DictTypeService {

    @Resource
    private DictTypeDao dictTypeDaoImpl;
    @Resource
    private DictDataDao dictDataDaoImpl;
    public static final String INSERT_LOCK = "insert";

    /**
     * 单条插入
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public int insert(DictType dictType) throws SQLException {
        int insert = 0;
        synchronized (INSERT_LOCK) {
            DictType dict = new DictType();
            dict.setCode(dictType.getCode());
            int countByDto = countByDto(dict);
            if (countByDto > 0) {
                throw new TransException("字典类型代码已存在！");
            }

            insert = dictTypeDaoImpl.insert(dictType);
        }
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public int insertList(DictType dictType) throws SQLException {
        int insertList = dictTypeDaoImpl.insertList(dictType.getDictTypes());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByDto(DictType dictType) throws SQLException {
        int deleteByDto = dictTypeDaoImpl.deleteByDto(dictType);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public int countAll() throws SQLException {
        int countAll = dictTypeDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public List<DictType> findAll() throws SQLException {
        List<DictType> findAll = dictTypeDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public int countByDto(DictType dictType) throws SQLException {
        int countByDto = dictTypeDaoImpl.countByDto(dictType);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public List<DictType> findByDto(DictType dictType) throws SQLException {
        List<DictType> findByDto = dictTypeDaoImpl.listByDto(dictType);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteById(Integer id) throws SQLException {
        int deleteById = dictTypeDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByIdList(ArrayList<Integer> ids) throws SQLException {
        int deleteByIdList = dictTypeDaoImpl.deleteByIdList(ids);
        return deleteByIdList;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public int updateById(DictType dictType) throws SQLException {
        int updateById = dictTypeDaoImpl.updateById(dictType);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticByIdList(DictType dictType) throws SQLException {
        int updateStaticByIdList = dictTypeDaoImpl.updateStaticByIdList(dictType);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangeByIdList(DictType dictType) throws SQLException {
        int updateChangeByIdList = dictTypeDaoImpl.updateChangeByIdList(dictType.getDictTypes());
        return updateChangeByIdList;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public int updatePartialById(DictType dictType) throws SQLException {
        int updatePartialById = dictTypeDaoImpl.updatePartialById(dictType);
        return updatePartialById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticPartialByIdList(DictType dictType) throws SQLException {
        int updateStaticPartialByIdList = dictTypeDaoImpl.updateStaticPartialByIdList(dictType);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangePartialByIdList(DictType dictType) throws SQLException {
        int updateChangePartialByIdList = dictTypeDaoImpl.updateChangePartialByIdList(dictType.getDictTypes());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public DictType findById(Integer id) throws SQLException {
        DictType findById = dictTypeDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<DictType> findByIdList(ArrayList<Integer> ids) throws SQLException {
        ArrayList<DictType> findByIdList = dictTypeDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Integer> findIdByDto(DictType dictType) throws SQLException {
        ArrayList<Integer> findIdByDto = dictTypeDaoImpl.findIdByDto(dictType);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     * 
     * @throws SQLException
     */
    @Override
    public void clear() throws SQLException {
        dictTypeDaoImpl.clear();
    }

    /**
     * 通过code查询
     * 
     * @param code
     * @return
     * @throws SQLException
     */
    @Override
    public DictType findByCode(String code) {
        return dictTypeDaoImpl.findByCode(code);
    }

    /**
     * 通过code列表删除(物理删除)
     * 
     * @param codes
     * @return
     * @throws SQLException
     */
    @Override
    @Transactional
    public int deleteByCodeList(ArrayList<String> codes) throws SQLException {
        dictDataDaoImpl.deleteByCodeList(codes);
        return dictTypeDaoImpl.deleteByCodeList(codes);
    }

}